package cn.wolfcode.crm.service.impl;

<<<<<<< HEAD
import cn.wolfcode.crm.domain.Attendance;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.AttendanceMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.service.IAttendanceService;
=======
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.EmployeeQueryObject;
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
import cn.wolfcode.crm.service.IEmployeeService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

<<<<<<< HEAD
import java.text.SimpleDateFormat;
import java.util.Calendar;
=======
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
import java.util.Date;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService{

    @Autowired
    private EmployeeMapper employeeMapper;
<<<<<<< HEAD

    @Autowired
    private AttendanceMapper attendanceMapper;
=======
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
    @Override
    public int deleteByPrimaryKey(Long id) {
        return employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Employee entity) {
        //保存入职时间/密码
        entity.setInputTime(new Date());
        String password = new Md5Hash("1",entity.getRealName(),1).toString();
        entity.setPassword(password);
        employeeMapper.insert(entity);
        //在保存完员工之后需要维护员工和角色之间的关系

        List<Role> roles = entity.getRoles();
        if(roles!=null){

            for (Role role : roles) {

                employeeMapper.saveEmployeeRoleRelation(entity.getId(),role.getId());
            }
        }

        return 0;
    }

    public static void main(String[] args) {
        String password = new Md5Hash("1","3",3).toString();
        System.out.printf(password);
    }
    @Override
    public Employee selectByPrimaryKey(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Employee entity) {
        employeeMapper.updateByPrimaryKey(entity);
        //删除员工和角色之间的旧关系
        employeeMapper.deleterEmployeeRoleRelation(entity.getId());
        //保存员工和角色之间的新关系
        List<Role> roles = entity.getRoles();
        if(roles!=null){

            for (Role role : roles) {

                employeeMapper.saveEmployeeRoleRelation(entity.getId(),role.getId());
            }
        }

        return 0;
    }

    @Override
    public PageResult query(EmployeeQueryObject qo) {
        int totalCount = employeeMapper.queryForCount(qo);
        if(totalCount == 0){
            return new PageResult();
        }
        List<Employee> list = employeeMapper.queryForList(qo);
        return new PageResult(totalCount,list);
    }

    @Override
    public int updateState(Long id) {

       return employeeMapper.updateState(id,Employee.STATE_LOCK);
    }

    @Override
    public List<Employee> querForList(EmployeeQueryObject qo) {
        return employeeMapper.queryForList(qo);
    }

    @Override
    public Employee getEmployeeByUsername(String username) {

        Employee employee = employeeMapper.getEmployeeByUsername(username);
        if(employee == null){
            return null;
        }
        return employee;
    }

<<<<<<< HEAD
    @Override
    public void changePassword(Long empId, String password) {
        try{

            Employee employee = employeeMapper.selectByPrimaryKey(empId);

            Md5Hash hash = new Md5Hash(password,employee.getRealName(),3);
            employee.setPassword(hash.toString());
            employeeMapper.updatePassword(employee);
        }catch (Exception e){

            e.printStackTrace();
        }


    }

    @Override
    public void goWork(Long empId) {
        Employee employee = employeeMapper.selectByPrimaryKey(empId);
        Attendance attendance = new Attendance();
        attendance.setEmployee(employee);
        attendance.setDepartment(employee.getDept());
        Date date = new Date();
        attendance.setCurrentDate(date);
        //设置上班的时间
        Calendar instance = Calendar.getInstance();
        //获取当前天的Calendar对象
        instance.setTime(date);

        instance.set(Calendar.HOUR_OF_DAY,9);
        instance.set(Calendar.MINUTE,0);
        instance.set(Calendar.SECOND,0);
        Date beginDate = instance.getTime();

        instance.set(Calendar.HOUR_OF_DAY,18);
        instance.set(Calendar.MINUTE,0);
        instance.set(Calendar.SECOND,0);
        Date endDate = instance.getTime();
        if(!date.after(beginDate)){

            attendance.setState(0);
        }else{
            attendance.setState(1);
        }
        attendance.setBeginTime(date);

        attendanceMapper.goWork(attendance);

    }

    @Override
    public void backWork(Long empId) {

        Employee employee = employeeMapper.selectByPrimaryKey(empId);
        Attendance attendance = new Attendance();
        attendance.setEmployee(employee);
        attendance.setDepartment(employee.getDept());
        Date date = new Date();
        attendance.setCurrentDate(date);
        //设置上班的时间
        Calendar instance = Calendar.getInstance();
        //获取当前天的Calendar对象
        instance.setTime(date);

        instance.set(Calendar.HOUR_OF_DAY,9);
        instance.set(Calendar.MINUTE,0);
        instance.set(Calendar.SECOND,0);
        Date beginDate = instance.getTime();

        instance.set(Calendar.HOUR_OF_DAY,18);
        instance.set(Calendar.MINUTE,0);
        instance.set(Calendar.SECOND,0);
        Date endDate = instance.getTime();
        if(date.before(endDate)){
            attendance.setState(2);

        }else{
            attendance.setState(0);
        }
        attendance.setEndTime(date);

        attendanceMapper.backWork(attendance);

    }


=======
>>>>>>> 524224bf820c6d6da01f752551ee21b728709493
}
